/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cw.biz;

import cw.controller.PuzzleJpaController;
import cw.entity.Puzzle;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VinhTC
 */
public class PuzzleBiz {

    PuzzleJpaController puzzController = new PuzzleJpaController();

    public PuzzleBiz() {
    }

    /**
     * puzzleNameList
     * @return List<String> 
     */
    public List<String> puzzleNameList() {
        List<String> lst = new ArrayList<String>();
        List<Puzzle> temp = puzzController.findPuzzleEntities();

        for (int i = 0; i < temp.size(); i++) {
            if (!temp.get(i).getCreated()) {
                lst.add(temp.get(i).getPuzzleName());
            }
        }
        return lst;
    }

    /**
     * findPuzzleByName
     * @param puzzleName
     * @return Puzzle
     */
    public Puzzle findPuzzleByName(String puzzleName) {
        Puzzle puzzle = new Puzzle();
        puzzle = puzzController.getPuzzleByName(puzzleName);
        return puzzle;
    }

    /**
     * findPuzzleById
     * @param puzzleId
     * @return Puzzle
     */
    public Puzzle findPuzzleById(int puzzleId) {
        Puzzle puzzle = new Puzzle();
        puzzle = puzzController.findPuzzle(puzzleId);
        return puzzle;
    }

    /**
     * updatePuzzle
     * @param puzzle
     * @return boolean
     */
    public boolean updatePuzzle(Puzzle puzzle) {
        boolean result = false;
        try {
            puzzController.edit(puzzle);

            result = true;
        } catch (Exception e) {
            Logger.getLogger(PuzzleBiz.class.getName()).log(Level.SEVERE, null, e);
        }
        return result;
    }

    /**
     * updatePuzzle
     * @param puzzle
     * @return boolean
     */
    public boolean createPuzzle(Puzzle puzzle) {
        boolean result = false;
        try {
            puzzController.create(puzzle);

            result = true;
        } catch (Exception e) {
            Logger.getLogger(PuzzleBiz.class.getName()).log(Level.SEVERE, null, e);
        }
        return result;
    }

    /**
     * deletePuzzle
     * @param puzzle
     * @return boolean
     */
    public boolean deletePuzzle(Puzzle puzzle) {
        boolean result = false;
        try {
            puzzController.destroy(puzzle.getPuzzleId());

            result = true;
        } catch (Exception e) {
            Logger.getLogger(PuzzleBiz.class.getName()).log(Level.SEVERE, null, e);
        }
        return result;
    }
    /**
     * getPuzzleByLevelSizeSubject
     * @param levelId
     * @return List<Puzzle>
     */
    public List<Puzzle> getPuzzleByLevelSizeSubject(int levelId, int sizeId, int subjectId) {
        List<Puzzle> lst = null;
        try {
            lst = puzzController.getPuzzleByLevelId(levelId, sizeId, subjectId);
        } catch (Exception e) {
            Logger.getLogger(PuzzleBiz.class.getName()).log(Level.SEVERE, null, e);
        }
        return lst;
    }

    /**
     * getPuzzleRandom
     * @param levelId
     * @return Puzzle
     */
    public Puzzle getPuzzleRandom(int levelId, int sizeId, int subjectId) {
        Puzzle puzz = new Puzzle();
        PuzzleBiz puzzBiz = new PuzzleBiz();
        List<Puzzle> lst = null;
        try {
            lst = puzzBiz.getPuzzleByLevelSizeSubject(levelId, sizeId, subjectId);
        } catch (Exception e) {
            Logger.getLogger(PuzzleBiz.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
        }
        System.out.println("size: " + lst.size());
        if (lst != null) {
            int start = 0;
            int end = lst.size()-1;
            System.out.println("Start: " + start + ", End: " + end);
            Random random = new Random();
            int index = getRandomInteger(start, end, random);

            for (int i = 0; i < lst.size(); i++) {
                if (i == index) {
                    puzz = lst.get(index);
                }
            }
        }

        return puzz;
    }

    /**
     * getPuzzleBySubjectId
     * @param subjectId
     * @return List<Puzzle>
     */
    public List<Puzzle> getPuzzleBySubjectId(int subjectId) {
        List<Puzzle> lst = puzzController.getPuzzleBySubjectId(subjectId);

        return lst;
    }

    /**
     * showRandomInteger
     * @param aStart
     * @param aEnd
     * @param aRandom
     */
    public int getRandomInteger(int aStart, int aEnd, Random aRandom) {
        if (aStart > aEnd) {
            throw new IllegalArgumentException("Start cannot exceed End.");
        }
        //get the range, casting to long to avoid overflow problems
        long range = (long) aEnd - (long) aStart + 1;
        // compute a fraction of the range, 0 <= frac < range
        long fraction = (long) (range * aRandom.nextDouble());
        int randomNumber = (int) (fraction + aStart);

        return randomNumber;
    }

    /**
     * checkPuzzleExist
     * @param puzzle
     * @return
     */
    public boolean checkPuzzleNotExist(String puzzleName){
        boolean result = false;
        try {
            if(findPuzzleByName(puzzleName) == null){
                result = true;
            }
        } catch (Exception e) {
            result = true;
        }
        return result;
    }

    public static void main(String[] args) {
        int start = 0;
        int end = 2;
        Random random = new Random();
        PuzzleBiz puzzBiz = new PuzzleBiz();
        int index = 0;
        for (int i = 0; i < end; i++) {
            index = puzzBiz.getRandomInteger(start, end, random);
        }
        System.out.println("index: " + index);
        Puzzle puzzle = new Puzzle();
        puzzle = puzzBiz.getPuzzleRandom(1, 1, 1);

        System.out.println("PuzzleName: " + puzzle.getPuzzleName());
    }
}
